package com.example.jacky.android_easeui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.jacky.android_easeui.adapter.HomeAdapter;
import com.example.jacky.android_easeui.base.BaseActivity;
import com.example.jacky.android_easeui.bean.DemoList;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jacky on 2016/10/8.
 * sounbus app-developer
 */

public class HomeActivity extends BaseActivity {
    private static final String TAG = "HomeActivity";
    @Bind(R.id.rcv)
    RecyclerView mRcv;

    private HomeAdapter mHomeAdapter;
    private ArrayList<String> mDemosList;
    private LinearLayoutManager mLinearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.BlankTheme);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initView();
        loadData();
    }

    private void initView() {
        //设置布局管理器
        mLinearLayoutManager = new LinearLayoutManager(this);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRcv.setLayoutManager(mLinearLayoutManager);
    }

    private void loadData() {
        mDemosList = new ArrayList<>();
        fillAndSortData(mDemosList);
        mHomeAdapter = new HomeAdapter(R.layout.item_home, mDemosList);
        mHomeAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                String name = mHomeAdapter.getItem(i);
                String className = getPackageName()+".demo." + name;
                if (name != null) {
                    try {
                        Class<? extends Activity> aClass = (Class<? extends Activity>) Class.forName(className);
                        forward(aClass);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        View emptyView = LayoutInflater.from(this).inflate(R.layout.adatper_emptyview, mRcv, false);
        mHomeAdapter.setEmptyView(emptyView);
        mRcv.setAdapter(mHomeAdapter);
    }

    private void fillAndSortData(ArrayList<String> demos) {
        // 填充数据
        for (int i = 0; i < DemoList.NAMES.length; i++) {
            String name = DemoList.NAMES[i];
            demos.add(name);
        }
    }


}
